package com.fuxi.cloud.common.util;

import cn.hutool.core.util.ObjectUtil;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * DesUtils
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-05-03
 */
public class DesUtils {
    public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";
    public static String appkey = "c306e6eb-fdba-11e7-9bb0-00163e0004bf";
    public static String sercret = "jZ0F9RTa5Y4NDZ95C4n38SuddBgtSw05";
    public static String DESIV;
    public static String key = "1559383106";

    public DesUtils() {
    }

    public static String encode(String key, String data) {
        try {
            String var2 = encode(key, data.getBytes());
            return var2;
        } catch (Exception var3) {
            var3.printStackTrace();
            return "";
        }
    }

    public static String encode1(String key, String data, String iv) throws Exception {
        return encode1(key, data.getBytes(), iv);
    }

    public static String encode(String key, byte[] data) throws Exception {
        try {
            DESKeySpec var2 = new DESKeySpec(key.getBytes());
            SecretKeyFactory var3 = SecretKeyFactory.getInstance("DES");
            SecretKey var4 = var3.generateSecret(var2);
            Cipher var5 = Cipher.getInstance("DES/CBC/PKCS5Padding");
            IvParameterSpec var6 = new IvParameterSpec(DESIV.getBytes());
            var5.init(1, var4, var6);
            byte[] var8 = var5.doFinal(data);
            return Base64.getEncoder().encodeToString(var8);
        } catch (Exception var9) {
            throw new Exception(var9);
        }
    }

    public static String encode1(String key, byte[] data, String sic) throws Exception {
        try {
            DESKeySpec var3 = new DESKeySpec(key.getBytes());
            SecretKeyFactory var4 = SecretKeyFactory.getInstance("DES");
            SecretKey var5 = var4.generateSecret(var3);
            Cipher var6 = Cipher.getInstance("DES/CBC/PKCS5Padding");
            IvParameterSpec var7 = new IvParameterSpec(sic.getBytes());
            var6.init(1, var5, var7);
            byte[] var9 = var6.doFinal(data);
            return Base64.getEncoder().encodeToString(var9);
        } catch (Exception var10) {
            throw new Exception(var10);
        }
    }

    public static byte[] decode(String key, byte[] data) throws Exception {
        Object var2 = null;

        try {
            DESKeySpec var3 = new DESKeySpec(key.getBytes());
            SecretKeyFactory var4 = SecretKeyFactory.getInstance("DES");
            SecretKey var5 = var4.generateSecret(var3);
            Cipher var6 = Cipher.getInstance("DES/CBC/PKCS5Padding");
            IvParameterSpec var7 = new IvParameterSpec(DESIV.getBytes());
            var6.init(2, var5, var7);
            byte[] var14 = var6.doFinal(data);
            return var14;
        } catch (Exception var12) {
            throw new Exception(var12);
        } finally {
            ;
        }
    }

    public static String decodeValue(String key, String data) {
        String var3 = null;

        try {
            byte[] var2 = decode(key, Base64.getDecoder().decode(data));
            if (ObjectUtil.isEmpty(var2)) {
                return "";
            }

            var3 = new String(var2);
            if (var3.equals("")) {
                throw new Exception();
            }
        } catch (Exception var5) {
            ;
        }

        return var3;
    }

    public static String getkey(String time) {
        StringBuffer var2 = new StringBuffer(md5Password(time, 1));
        String var3 = var2.toString().substring(0, 8);
        String var1 = var3.toUpperCase() + appkey;
        StringBuffer var4 = new StringBuffer(md5Password(var1, 1));
        String var5 = var4.toString();
        var1 = var5.substring(12, 20).toLowerCase();
        return var1;
    }

    public static String getiv(String time) {
        StringBuffer var2 = new StringBuffer(md5Password(time, 1));
        String var3 = var2.substring(12, 20);
        String var1 = var3.toLowerCase() + sercret;
        StringBuffer var4 = new StringBuffer(md5Password(var1, 1));
        var1 = var4.substring(24, 32).toUpperCase();
        return var1;
    }

    public static String md5Password(String password, int code) {
        try {
            MessageDigest var2 = MessageDigest.getInstance("md5");
            byte[] var3 = var2.digest(password.getBytes());
            StringBuffer var4 = new StringBuffer();
            byte[] var5 = var3;
            int var6 = var3.length;

            for(int var7 = 0; var7 < var6; ++var7) {
                byte var8 = var5[var7];
                int var9 = var8 & 255;
                String var10 = Integer.toHexString(var9);
                if (var10.length() == 1) {
                    var4.append("0");
                }

                var4.append(var10);
            }

            return var4.toString();
        } catch (NoSuchAlgorithmException var11) {
            var11.printStackTrace();
            return "";
        }
    }

    public static String decode(String str) {
        DESIV = getiv(key);
        String var1 = getkey(key);
        return encode(var1, str);
    }

    public static String encode(String str) {
        DESIV = getiv(key);
        String var1 = getkey(key);
        return decodeValue(var1, str);
    }

    public static void main(String[] args) throws Exception {
        String var1 = "18739941307";
        String var2 = key;
        DESIV = getiv(var2);
        String var3 = getkey(var2);
        String var4 = encode(var3, var1);
        System.out.println("密文" + var4);
        String var5 = getkey(var2);
        System.out.println("333:" + decodeValue(var5, var4));
        System.out.println("解码" + getTel(var4, key));
        System.out.println("加密" + decode("root"));
        System.out.println("解密" + encode("SMy3f94QGFM="));
    }

    public static String getTel(String str_tel, String str_time) {
        DESIV = getiv(str_time);
        String var2 = getkey(str_time);
        return decodeValue(var2, str_tel);
    }
}
